﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;

/// <summary>
///SqlDBClass 的摘要说明
/// </summary>
public class SqlDBClass
{
    public static SqlConnection conn = new SqlConnection();
    public static SqlCommand command = new SqlCommand();
    public SqlDBClass()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
        //conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
        //try
        //{
        //    conn.Open();
        //}
        //catch
        //{
        //}
    }

    public void Dispose()
    {
        try
        {
            conn.Close();
            conn.Dispose();
        }
        catch
        {

        }
    }

    private static void closeConnection()
    {
        if (conn.State == ConnectionState.Open)
        {
            command.Dispose();
            conn.Close();
            conn.Dispose();
        }
    }//关闭数据库


    private static void openConnection()
    {
        if (conn.State == ConnectionState.Closed)
        {
            conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
            command = conn.CreateCommand();
            try
            {
                conn.Open();
            }
            catch (Exception e)
            {  }
        }

    }//打开数据库


    /// <summary>
    /// 用于select语句，返回DataSet
    /// </summary>
    /// <param name="strSql"></param>
    /// <returns></returns>
    public DataSet execSql(string strSql)
    {
        openConnection();
        DataSet SqlDS = new DataSet();
        //command = conn.CreateCommand();
        try
        {
            command.CommandType = CommandType.Text;
            command.CommandText = strSql;
            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = command;
            adapter.Fill(SqlDS);
        }
        catch (Exception e)
        {

        }
        finally
        {
            closeConnection();
        }
        return SqlDS;
    }


    /// <summary>
    /// 用于insert,update,delete语句的事务
    /// </summary>
    /// <param name="SQLStringList"></param>
    /// <returns></returns>
    public bool execSqlTran(string[] SQLStringList)
    {

        openConnection();
        bool result = false;
        SqlTransaction tx = conn.BeginTransaction();
        command.Transaction = tx;
        try
        {
            for (int n = 0; n < SQLStringList.Length; n++)
            {
                string strsql = SQLStringList[n];
                if (strsql.Trim().Length > 1)
                {
                    command.CommandText = strsql;
                    int i = command.ExecuteNonQuery();
                    if (i != 0)
                        result = true;
                    else
                        result = false;
                }
            }
            tx.Commit();
        }
        catch (System.Data.SqlClient.SqlException E)
        {
            result = false;
            tx.Rollback();
            throw new Exception(E.Message);
        }
        finally
        {
            closeConnection();
        }
        return result;
    }


    /// <summary>
    /// 用于insert,update,delete语句，影响行数不为0则为执行成功
    /// </summary>
    /// <param name="strSql"></param>
    /// <returns></returns>
    public bool execSqlNoQuery(string strSql)
    {
        openConnection();
        bool result = false;
        //command = conn.CreateCommand();
        try
        {
            command.CommandType = CommandType.Text;
            command.CommandText = strSql;
            int i = command.ExecuteNonQuery();
            if (i != 0)
                result = true;
            else
                result = false;
            //return true;
        }
        catch (Exception e)
        {
            result = false;
            return false;
        }
        finally
        {
            closeConnection();
        }
        return result;
    }
}